//
// $Id$

package zachg.survival;

import java.awt.AlphaComposite;
import java.awt.geom.Ellipse2D;

import com.samskivert.swing.ShapeIcon;
import com.threerings.crowd.client.PlaceView;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.util.CrowdContext;

import com.threerings.parlor.game.client.GameController;
import com.threerings.toybox.util.ToyBoxContext;

import com.threerings.util.Name;
import com.threerings.parlor.turn.client.TurnGameController;
import com.threerings.parlor.turn.client.TurnGameControllerDelegate;

/**
 * Manages the client side mechanics of the game.
 */
public class SurvivalController extends GameController
implements TurnGameController
{
	public SurvivalController ()
    {
        addDelegate(_turndel = new TurnGameControllerDelegate(this));
    }

    // from interface TurnGameController
    public void turnDidChange (Name turnHolder)
    {
        // if it's our turn, activate piece placement
        _panel.bview.setPlacingMode(_turndel.isOurTurn() ? _gameCharacterIndex : -1);
        _panel.bview.updateGameCharacterSprites(_turndel.isOurTurn(), _ctx.getClient().getClientObject().username );
        
        //make the temporary move sprite in the view invisible until the player clicks a location
        _panel.bview._nextMoveSprite._comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0f);         

        //update the view panel with the latest information
        _panel.bview.updateSidePanelOptions(null);
        _panel.bview.updateSidePanelInformation();
        
        _panel.bview.setPlaceHolderPosition();
    	
        //setting the controller on the turnDisplay actually forces the scores to update.
        _panel.turnDisplay.setGameobj(_gameobj);
    }
	
	/**
     * Requests that we leave the game and return to the lobby.
     */
    public void backToLobby ()
    {
        _ctx.getLocationDirector().moveBack();
    }

    @Override // from PlaceController
    public void willEnterPlace (PlaceObject plobj)
    {
        super.willEnterPlace(plobj);

        // get a casted reference to our game object
        _gameobj = (SurvivalObject)plobj;

        // if it's our turn, activate piece placement
        _panel.bview.setPlacingMode(_turndel.isOurTurn() ? _gameCharacterIndex : -1);
    }
    
    @Override // from PlaceController
    public void didLeavePlace (PlaceObject plobj)
    {
        super.didLeavePlace(plobj);

        // clear out our game object reference
        _gameobj = null;
    }

    @Override // from PlaceController
    protected PlaceView createPlaceView (CrowdContext ctx)
    {
        _panel = new SurvivalPanel((ToyBoxContext)ctx, this);
        return _panel;
    }

    @Override // from GameController
    protected void gameDidStart ()
    {
        super.gameDidStart();
        
        // here we can set up anything that should happen at the start of the
        // game        

        //sets the initial turn display scores
        //NOTE: We do this here and not in willEnterPlace because our gameCharacters aren't created at that point.
    	_panel.turnDisplay.setGameobj(_gameobj);
    	
        //set indexes on all player characters
        for(int i = 0 ; i < _gameobj.getPlayerCount(); i++){
        	int _gameCharacterIndex = _gameobj.getPlayerIndex(_gameobj.players[i]);
        	// determine index in player list array, NOT in array of gamecharacters in view.
        	//set index on the actual game object for later reference in GameCharacterSprite
        	SurvivalObject.GameCharacter gameCharacter = _gameobj.getGameCharacterFromIndex(_gameCharacterIndex);
        	gameCharacter.playerIndex = _gameCharacterIndex;
        	_gameobj.updateGameCharacters(gameCharacter);
        }
        
        //set icons in panel. NOTE: we don't do this in the panel itself because 
        //it has to be delayed until startup, which is after initialization 
        Ellipse2D lips = new Ellipse2D.Float(0, 0, 12, 12);
        ShapeIcon[] playerIcons = new ShapeIcon[_gameobj.getPlayerCount()];
        for(int i = 0 ; i < _gameobj.getPlayerCount(); i++){
        	playerIcons[i] = new ShapeIcon(lips, SurvivalObject.PlayerColors[i], null);
        }
        _panel.turnDisplay.setPlayerIcons(playerIcons); 
        
    }

    @Override // from GameController
    protected void gameDidEnd ()
    {
        super.gameDidEnd();

        // here we can clear out anything that needs to be cleared out at the
        // end of a game
    }

    public void endTurn ()
    {
        // tell the server we want to place our game character here, but do nothing else;
        _gameobj.manager.invoke("endTurn",_panel.bview._nextMoveSprite.getGameCharacter(), _gameobj.getGameCharacterFromIndex(_turndel.getTurnHolderIndex()) );
    }    
        
    /**
     * a character mined (simply triggers a server method)
     */
    public void gameCharacterMined ()
    {
    	//check if we can mine here, just because the mine locations are stuck in the view
    	// this is bad practice, and I've done it just because I've utterly run out of 
    	//time to properly move the _mineLocations into SurvivalObject 
		_gameobj.manager.invoke(
				"gameCharacterMined", 
				_panel.bview._nextMoveSprite.getGameCharacter(), 
				_gameobj.getGameCharacterFromIndex(_turndel.getTurnHolderIndex())
		);
    }
    
    /**
     * a character attacked (simply triggers a server method)
     */
    public void gameCharacterAttack ()    
    {
    	_gameobj.manager.invoke(
    			"gameCharacterAttack", 
    			_panel.bview._nextMoveSprite.getGameCharacter(), 
    			_gameobj.getGameCharacterFromIndex(_turndel.getTurnHolderIndex()) 
    	);
    }

    /** Our game panel. */
    public SurvivalPanel _panel;

    /** Our game distributed object. */
    protected SurvivalObject _gameobj;

    /** Handles turn-game related stuff. */
    protected TurnGameControllerDelegate _turndel;

    /** Our game character ID. */
    protected int _gameCharacterIndex;
    
}
